package com.example.yls.demoa;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements IOnTimeChangeListener{
    private Button btnStar;
    private TextView txtTime;
    private MyReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("MainActivity", "thread id= " + Thread.currentThread().getId());


        receiver = new MyReceiver(MainActivity.this);
        IntentFilter filter = new IntentFilter();
        filter.addAction("TIME_CHANGE");
        registerReceiver(receiver,filter);

        btnStar = findViewById(R.id.btn_star);
        btnStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                startService(intent);
            }
        });

        txtTime = findViewById(R.id.txt_time);
    }

    @Override
    public void show(String str) {
        txtTime.setText(str);
    }
/*    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String timeStr = intent.getStringExtra("time");
            txtTime.setText(timeStr);
        }
}*/

}